{% extends "base.html" %}

{% block title %}Newest Comments{% endblock title %}

{% block content %}
  {% cache 60*5, current_user.id, forum %}
<style>
.custom-table {
  table-layout: fixed;
  width: 100%;
}

.custom-table th:first-child, .custom-table td:first-child {
  width: 15%;
}

.custom-table th:nth-child(3), .custom-table td:nth-child(3) {
  width: 15%;
}

{% if current_user.is_authenticated and current_user.has_role('moderator') %}
  .custom-table th:nth-child(4), .custom-table td:nth-child(4) {
    width: 15%;
  }
{% endif %}

</style>
{%set posts = comments_for(forum) %}

{% macro mark_post_button(post, action, label) %}
  {% if current_user.is_authenticated and current_user.has_role('moderator') %}
    <form method="post" action="/comment/{{ post.id }}/{{ action }}">
      <input type="hidden" name="back_to" value="comments">
      <button class="btn btn-lg btn-primary" type="submit">{{ label }}</button>
    </form>
  {% endif %}
{% endmacro %}

<h1>Newest Comments</h1>

<table class="table table-condensed custom-table">
  <thead>
    <tr>
      <th>name</th>
      <th>message</th>
      <th>created_at</th>
      {% if current_user.is_authenticated and current_user.has_role('moderator') %}
        <th></th>
      {% endif %}
    </tr>
  </thead>
  <tbody>
    {% for post in posts if ((not post.is_spam) and (not post.is_deleted)) %}
      <tr>
        <td>
          {{ post.author.name }}
          <br />
          <a href="{{ post.thread.link_path }}">{{ post.thread.link_path }}</a>
        </td>
        <td>{{ post.message_html | safe }}</td>
        <td>
          {{ post.created_at.strftime('%Y-%m-%d %H:%M') }}
        </td>
        {% if current_user.is_authenticated and current_user.has_role('moderator') %}
          <td>
            {{ mark_post_button(post, 'spam', 'Spam') }}
            {{ mark_post_button(post, 'delete', 'Delete') }}

            {% if post.is_approved is none %}
              {{ mark_post_button(post, 'approve', 'Approve') }}
            {% elif post.is_approved %}
              {{ mark_post_button(post, 'unapprove', 'UnApprove') }}
            {% else %}
              {{ mark_post_button(post, 'approve', 'Approve') }}
            {% endif %}
          </td>
        {% endif %}
      </tr>
    {% endfor %}
  </tbody>
</table>




  {% endcache %}
{% endblock %}